const tool = require('../tools/tool')
// const notification = require('./notification')
// const sendMessage = require('./sendMessage')
// const patrol = require('./patrol')
// const campuses = require('./campuses')
// const categories = require('./categories')
// const settings = require('./settings')
// const { notification, sendMessage, patrol, campuses, categories, settings,  } = tool.methods
const config = require('../../config')

async function canSendMessage() {
    return (await tool.methods.settings().getItem('sendMessage')) === true
}

function getOperation(name, data) {
    return JSON.stringify({
        name, data
    })
}

module.exports = {
    patrol: {
        async add(patrolId) {
            setTimeout(async () => {
                try {
                    let re = await tool.methods.patrol().get.single(patrolId, undefined, true)
                    await tool.methods.notification().send.toHandleGroup(re.campusId, re.categoryId, `您有新的巡查任务待处理【#${re.id}】`, '系统', getOperation('openPatrol', patrolId), {
                        patrolId
                    })

                    if (await canSendMessage()) {
                        console.log(123)
                        tool.methods.sendMessage().toHandleGroup(re.campusId, re.categoryId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                            return `您有新的巡查任务待处理【#${re.id}】

${infoStr}`
                        }))
                    }
                } catch (error) {
                    console.log(error)
                }
            }, 1000 * 60 * (await tool.methods.settings().getItem('newPatrolSendNotificationWaitTime')));
        },
        async delay(patrolId) {
            try {
                let re = await tool.methods.patrol().get.single(patrolId, undefined, true)
                await tool.methods.notification().send.toHandleGroup(re.campusId, re.categoryId, `巡查【#${re.id}】被负责人推迟`, '系统', getOperation('openPatrol', patrolId), {
                    patrolId
                })
                await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】被负责人推迟`, '系统', getOperation('openPatrol', patrolId), {
                    patrolId
                })

                if (await canSendMessage()) {
                    tool.methods.sendMessage().toHandleGroup(re.campusId, re.categoryId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                        return `巡查【#${re.id}】被负责人推迟

${infoStr}`
                    }))
                    tool.methods.sendMessage().toPatrolCreater(patrolId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                        return `您的巡查【#${re.id}】被负责人推迟

${infoStr}`
                    }))
                }
            } catch (error) {
                // console.log(error)
            }
        },
        async handle(patrolId) {
            try {
                let re = await tool.methods.patrol().get.single(patrolId, undefined, true)
                if (re.status == 3) {
                    await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】已完成，请评价`, '系统', getOperation('openPatrol', patrolId), {
                        patrolId
                    })

                    if (await canSendMessage()) {
                        tool.methods.sendMessage().toPatrolCreater(patrolId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                            return `您的巡查【#${re.id}】已完成，请评价

${infoStr}`
                        }))
                    }
                }
                if (re.status == 4) {
                    await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】已完成`, '系统', getOperation('openPatrol', patrolId), {
                        patrolId
                    })

                    if (await canSendMessage()) {
                        tool.methods.sendMessage().toPatrolCreater(patrolId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                            return `您的巡查【#${re.id}】已完成

${infoStr}`
                        }))
                    }
                }
                if (re.status == 5) {
                    await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】被负责人拒绝处理`, '系统', getOperation('openPatrol', patrolId), {
                        patrolId
                    })

                    if (await canSendMessage()) {
                        tool.methods.sendMessage().toPatrolCreater(patrolId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                            return `您的巡查【#${re.id}】被负责人拒绝处理

${infoStr}`
                        }))
                    }
                }
            } catch (error) {
                // console.log(error)
            }
        },
        async review(patrolId) {
            try {
                let re = await tool.methods.patrol().get.single(patrolId, undefined, true)
                await tool.methods.notification().send.toHandleGroup(re.campusId, re.categoryId, `巡查【#${re.id}】被审核人驳回重新处理`, '系统', getOperation('openPatrol', patrolId), {
                    patrolId
                })
                await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】被审核人驳回重新处理`, '系统', getOperation('openPatrol', patrolId), {
                    patrolId
                })

                if (await canSendMessage()) {
                    tool.methods.sendMessage().toHandleGroup(re.campusId, re.categoryId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                        return `巡查【#${re.id}】被审核人驳回重新处理

${infoStr}`
                    }))
                    tool.methods.sendMessage().toPatrolCreater(patrolId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                        return `您的巡查【#${re.id}】被审核人驳回重新处理

${infoStr}`
                    }))
                }
            } catch (error) {
                // console.log(error)
            }
        },
        async priority(patrolId, userId) {
            try {
                let re = await require('./patrol').get.single(patrolId, undefined, true)
                let userData = await require('./user').getSingleUserInfoByUserId(userId)
                if (!userData) throw '用户不存在'
                await tool.methods.notification().send.toHandleGroup(re.campusId, re.categoryId, `请尽快处理巡查【#${re.id}】`, userData.username, getOperation('openPatrol', patrolId), {
                    patrolId
                })

                if (await canSendMessage()) {
                    tool.methods.sendMessage().toHandleGroup(re.campusId, re.categoryId, await tool.methods.sendMessage().getPatrolInMessageFormat(patrolId, (infoStr, singleItem) => {
                        return `请尽快处理巡查【#${re.id}】

${infoStr}`
                    }))
                }
            } catch (error) {
                console.log(error)
            }
        },
        edit: {
            async campusCategory(patrolId) {
                try {
                    let re = await tool.methods.patrol().get.single(patrolId, undefined, true)
                    await tool.methods.notification().send.toPatrolCreater(patrolId, `您的巡查【#${re.id}】被相关负责人修改了校区类别`, '系统', getOperation('openPatrol', patrolId), {
                        patrolId
                    })
                } catch (error) {
                    // console.log(error)
                }
            },
        }
    },
    async genExcel(userId, fileName) {
        await tool.methods.notification().send.toUser.byUserId(userId, `已生成表格文件，在通知页面复制下载链接`, '系统', getOperation('downloadFile', `${config.serverURL}/file/download/${fileName}`))

        if (await canSendMessage()) {
            tool.methods.sendMessage().toUser.byUserId(userId, `已生成表格文件，下载链接：${`${config.serverURL}/file/download/${fileName}`}`)
        }
    }
}